package com.jevons.muffin.widget.zloading;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;


import com.jevons.muffin.R;
import com.jevons.muffin.widget.CustomToast;
import com.jevons.muffin.widget.LoadingView;

import java.lang.ref.WeakReference;

/**
 * Created by zyao89 on 2017/10/9.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: https://zyao89.cn
 */

public class ZLoadingDialog implements LoadingView {
    private final WeakReference<Context> mContext;
    private final int mThemeResId;
    private Z_TYPE mLoadingBuilderType;
    private int mLoadingBuilderColor;
    private String mHintText;
    private float mHintTextSize = -1;
    private int mHintTextColor = -1;
    private boolean mCancelable = true;
    private boolean mCanceledOnTouchOutside = true;
    private double mDurationTimePercent = 1.0f;
    private int mDialogBackgroundColor = -1;
    private Dialog mZLoadingDialog;

    public ZLoadingDialog(@NonNull Context context) {
        this(context, R.style.z_alert_dialog);
    }

    public ZLoadingDialog(@NonNull Context context, int themeResId) {
        this.mContext = new WeakReference<>(context);
        this.mThemeResId = themeResId;
    }

    public ZLoadingDialog setLoadingBuilder(@NonNull Z_TYPE type) {
        this.mLoadingBuilderType = type;
        return this;
    }

    public ZLoadingDialog setLoadingColor(@ColorInt int color) {
        this.mLoadingBuilderColor = color;
        return this;
    }

    public ZLoadingDialog setHintText(String text) {
        this.mHintText = text;
        return this;
    }

    /**
     * 设置了大小后，字就不会有动画了。
     *
     * @param size 大小
     * @return
     */
    public ZLoadingDialog setHintTextSize(float size) {
        this.mHintTextSize = size;
        return this;
    }

    public ZLoadingDialog setHintTextColor(@ColorInt int color) {
        this.mHintTextColor = color;
        return this;
    }

    public ZLoadingDialog setDialogCancelable(boolean cancelable) {
        mCancelable = cancelable;
        return this;
    }

    public ZLoadingDialog setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        mCanceledOnTouchOutside = canceledOnTouchOutside;
        return this;
    }

    public ZLoadingDialog setDurationTime(double percent) {
        this.mDurationTimePercent = percent;
        return this;
    }

    public ZLoadingDialog setDialogBackgroundColor(@ColorInt int color) {
        this.mDialogBackgroundColor = color;
        return this;
    }

    private @NonNull View createContentView() {
        if (isContextNotExist()) {
            throw new RuntimeException("Context is null...");
        }
        return View.inflate(this.mContext.get(), R.layout.dialog_z_loading, null);
    }

    public Dialog create() {
        if (isContextNotExist()) {
            throw new RuntimeException("Context is null...");
        }
        if (mZLoadingDialog != null) {
            cancel();
        }
        mZLoadingDialog = new MyDialog(this.mContext.get(), this.mThemeResId);
        View contentView = createContentView();
        LinearLayout zLoadingRootView = (LinearLayout) contentView.findViewById(R.id.z_loading);

        // init color
        if (this.mDialogBackgroundColor != -1) {
            final Drawable drawable = zLoadingRootView.getBackground();
            if (drawable != null) {
                drawable.setAlpha(Color.alpha(this.mDialogBackgroundColor));
                drawable.setColorFilter(this.mDialogBackgroundColor, PorterDuff.Mode.SRC_ATOP);
            }
        }

        ZLoadingView zLoadingView = (ZLoadingView) contentView.findViewById(R.id.z_loading_view);
        TextView zCustomTextView = (TextView) contentView.findViewById(R.id.z_custom_text_view);
        if (this.mHintTextSize > 0 && !TextUtils.isEmpty(mHintText)) {
            zCustomTextView.setVisibility(View.VISIBLE);
            zCustomTextView.setText(mHintText);
            zCustomTextView.setTextSize(this.mHintTextSize);
            zCustomTextView.setTextColor(this.mHintTextColor == -1 ? this.mLoadingBuilderColor : this.mHintTextColor);
        }
        zLoadingView.setLoadingBuilder(this.mLoadingBuilderType);
        // 设置间隔百分比
        if (zLoadingView.mZLoadingBuilder != null) {
            zLoadingView.mZLoadingBuilder.setDurationTimePercent(this.mDurationTimePercent);
        }
        zLoadingView.setColorFilter(this.mLoadingBuilderColor, PorterDuff.Mode.SRC_ATOP);
        mZLoadingDialog.setContentView(contentView);
        mZLoadingDialog.setCancelable(this.mCancelable);
        if (this.mCancelable) {
            mZLoadingDialog.setCanceledOnTouchOutside(this.mCanceledOnTouchOutside);
        } else {
            mZLoadingDialog.setCanceledOnTouchOutside(false);
        }
        return mZLoadingDialog;
    }

    @Override
    public void setMessage(CharSequence message) {
        setHintText(message.toString());
    }

    @Override
    public boolean isShowing() {
        if (mZLoadingDialog != null) {
            return mZLoadingDialog.isShowing();
        }
        return false;
    }

    public void show() {
        if (mZLoadingDialog != null) {
            mZLoadingDialog.show();
        } else {
            final Dialog zLoadingDialog = create();
            zLoadingDialog.show();
        }
    }

    public void cancel() {
        if (mZLoadingDialog != null) {
            mZLoadingDialog.cancel();
        }
        mZLoadingDialog = null;
    }

    public void dismiss() {
        if (mZLoadingDialog != null) {
            mZLoadingDialog.dismiss();
        }
        mZLoadingDialog = null;
    }

    @Override
    public void setCancelable(boolean flag) {
        mCancelable = flag;
        if (mZLoadingDialog != null) {
            mZLoadingDialog.setCancelable(false);
        }
    }

    private boolean isContextNotExist() {
        Context context = this.mContext.get();
        return context == null;
    }

    @Override
    public void setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
        if (mZLoadingDialog != null) {
            mZLoadingDialog.setOnDismissListener(dismissListener);
        }
    }


    private class MyDialog extends Dialog {

        public MyDialog(@NonNull Context context) {
            super(context);
        }

        public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
            super(context, themeResId);
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (!mCancelable) {
                CustomToast.shortShow("正在请求，请稍等");
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }

    }

}
